//
//  MyArticleController.m
//  CaiPiao
//
//  Created by l wb on 2018/5/22.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "MyArticleController.h"
#import "BBSCell.h"
#import "BBSDetailController.h"
@interface MyArticleController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *dataArr;
@end

@implementation MyArticleController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"我的帖子";
    [self getMyArticleDataFromNet];
    
}

-(void)getMyArticleDataFromNet{
    BmobQuery *query = [BmobQuery queryWithClassName:@"Articles"];
    [query orderByDescending:@"createdAt"];
    [query whereKey:@"userID" equalTo:[BmobUser currentUser].objectId];
    [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (!error) {
            self.dataArr = array;
            [self.tableView reloadData];
        }
    }];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 261;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BmobObject *object;
   
    object = self.dataArr[indexPath.row];
   
    BBSCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BBSCell"];
    
    
    [cell.headImg sd_setImageWithURL:[NSURL URLWithString:[object objectForKey:@"headImgUrl"] ] placeholderImage:[UIImage imageNamed:@"avatar"]];
    
    
    cell.nameLab.text = [object objectForKey:@"name"];
    
    cell.dateLab.text = [object objectForKey:@"createdAt"];
    
    cell.contentLab.text = [object objectForKey:@"content"];
    [cell.showImg sd_setImageWithURL:[NSURL URLWithString:[object objectForKey:@"showImg" ]] placeholderImage:[UIImage imageNamed:@"帖子占位图"]];
  
    
    cell.likeLab.text = [object objectForKey:@"likeCount"];
    cell.commentCountLab.text = [object objectForKey:@"commentCount"];
    
    cell.deleteBtn.hidden = NO;
    cell.deleteBtn.tag = indexPath.row;
    [cell.deleteBtn addTarget:self action:@selector(deleteMyArticleClickAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return cell;
}
-(void)deleteMyArticleClickAction:(UIButton *)btn{
    BmobObject *object = self.dataArr[btn.tag];
    [object deleteInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            [EasyTextView showText:@"删除成功"];
            [self getMyArticleDataFromNet];
        }
    }];
    
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BmobObject *model;
    
    model = self.dataArr[indexPath.row];
    
    BBSDetailController *detail = [[UIStoryboard storyboardWithName:@"BBSStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"BBSDetailController"];
    detail.model = model;
    [self.navigationController pushViewController:detail animated:YES];
    
}

@end
